#include "bits/stdc++.h"
using namespace std;
// n >= m
// C(n, m) 同余 C(n % p, m % p) * C(n / p, m / p) % p
// 应用P比较小的情况
// Lucas要求P为质数
struct Lucas {
	Lucas(int P_) : P(P_), fac_(P_ + 1), inv_(P_ + 1) {
		fac_[0] = inv_[0] = 1;
		int nP = P - 1;
		for (int i = 1; i <= nP; i++) {
			fac_[i] = 1ll * fac_[i - 1] * i % P;
		}
		inv_[nP] = power(fac_[nP], P - 2);
		for (int i = nP - 1; i >= 1; i--) {
			inv_[i] = 1ll * inv_[i + 1] * (i + 1) % P;
		}
	}

	int power(int a, int b) {
		int ret = 1;
		while (b > 0) {
			if (b & 1) {
				ret = 1ll * ret * a % P;
			}
			a = 1ll * a * a % P;
			b >>= 1;
		}
		return ret;
	}

	int C(int n, int m) {
		if (n < m || m < 0 || n < 0) {
			return 0;
		}
		return (int)(1ll * fac_[n] * inv_[m] % P * inv_[n - m] % P);
	}

	int work(int n, int m) {
		if (n == 0) {
			return 1;
		}
		return (int)(1ll * work(n / P, m / P) * C(n % P, m % P) % P);
	}

	vector<int> fac_, inv_;
	int P;
};

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m, p;
		cin >> n >> m >> p;
		Lucas lucas(p);
		cout << lucas.work(n + m, n) << '\n';
	}
	return 0;
}